
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Performance Tips for Frontend Authors &mdash; LLVM 3.7 documentation</title>
    
    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.7',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
    <link rel="next" title="MCJIT Design and Implementation" href="../MCJITDesignAndImplementation.html" />
    <link rel="prev" title="The Often Misunderstood GEP Instruction" href="../GetElementPtr.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body role="document">
<div class="logo">
  <a href="../index.html">
    <img src="../_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../MCJITDesignAndImplementation.html" title="MCJIT Design and Implementation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../GetElementPtr.html" title="The Often Misunderstood GEP Instruction"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="../index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <div class="section" id="performance-tips-for-frontend-authors">
<h1>Performance Tips for Frontend Authors<a class="headerlink" href="#performance-tips-for-frontend-authors" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#abstract" id="id1">Abstract</a></li>
<li><a class="reference internal" href="#avoid-loads-and-stores-of-large-aggregate-type" id="id2">Avoid loads and stores of large aggregate type</a></li>
<li><a class="reference internal" href="#prefer-zext-over-sext-when-legal" id="id3">Prefer zext over sext when legal</a></li>
<li><a class="reference internal" href="#zext-gep-indices-to-machine-register-width" id="id4">Zext GEP indices to machine register width</a></li>
<li><a class="reference internal" href="#other-things-to-consider" id="id5">Other things to consider</a></li>
<li><a class="reference internal" href="#adding-to-this-document" id="id6">Adding to this document</a></li>
</ul>
</div>
<div class="section" id="abstract">
<h2><a class="toc-backref" href="#id1">Abstract</a><a class="headerlink" href="#abstract" title="Permalink to this headline">¶</a></h2>
<p>The intended audience of this document is developers of language frontends
targeting LLVM IR. This document is home to a collection of tips on how to
generate IR that optimizes well.  As with any optimizer, LLVM has its strengths
and weaknesses.  In some cases, surprisingly small changes in the source IR
can have a large effect on the generated code.</p>
</div>
<div class="section" id="avoid-loads-and-stores-of-large-aggregate-type">
<h2><a class="toc-backref" href="#id2">Avoid loads and stores of large aggregate type</a><a class="headerlink" href="#avoid-loads-and-stores-of-large-aggregate-type" title="Permalink to this headline">¶</a></h2>
<p>LLVM currently does not optimize well loads and stores of large <a class="reference internal" href="../LangRef.html#t-aggregate"><span>aggregate
types</span></a> (i.e. structs and arrays).  As an alternative, consider
loading individual fields from memory.</p>
<p>Aggregates that are smaller than the largest (performant) load or store
instruction supported by the targeted hardware are well supported.  These can
be an effective way to represent collections of small packed fields.</p>
</div>
<div class="section" id="prefer-zext-over-sext-when-legal">
<h2><a class="toc-backref" href="#id3">Prefer zext over sext when legal</a><a class="headerlink" href="#prefer-zext-over-sext-when-legal" title="Permalink to this headline">¶</a></h2>
<p>On some architectures (X86_64 is one), sign extension can involve an extra
instruction whereas zero extension can be folded into a load.  LLVM will try to
replace a sext with a zext when it can be proven safe, but if you have
information in your source language about the range of a integer value, it can
be profitable to use a zext rather than a sext.</p>
<p>Alternatively, you can <a class="reference internal" href="../LangRef.html#range-metadata"><span>specify the range of the value using metadata</span></a> and LLVM can do the sext to zext conversion for you.</p>
</div>
<div class="section" id="zext-gep-indices-to-machine-register-width">
<h2><a class="toc-backref" href="#id4">Zext GEP indices to machine register width</a><a class="headerlink" href="#zext-gep-indices-to-machine-register-width" title="Permalink to this headline">¶</a></h2>
<p>Internally, LLVM often promotes the width of GEP indices to machine register
width.  When it does so, it will default to using sign extension (sext)
operations for safety.  If your source language provides information about
the range of the index, you may wish to manually extend indices to machine
register width using a zext instruction.</p>
</div>
<div class="section" id="other-things-to-consider">
<h2><a class="toc-backref" href="#id5">Other things to consider</a><a class="headerlink" href="#other-things-to-consider" title="Permalink to this headline">¶</a></h2>
<ol class="arabic simple">
<li>Make sure that a DataLayout is provided (this will likely become required in
the near future, but is certainly important for optimization).</li>
<li>Add nsw/nuw flags as appropriate.  Reasoning about overflow is
generally hard for an optimizer so providing these facts from the frontend
can be very impactful.</li>
<li>Use fast-math flags on floating point operations if legal.  If you don&#8217;t
need strict IEEE floating point semantics, there are a number of additional
optimizations that can be performed.  This can be highly impactful for
floating point intensive computations.</li>
<li>Use inbounds on geps.  This can help to disambiguate some aliasing queries.</li>
<li>Add noalias/align/dereferenceable/nonnull to function arguments and return
values as appropriate</li>
<li>Mark functions as readnone/readonly or noreturn/nounwind when known.  The
optimizer will try to infer these flags, but may not always be able to.
Manual annotations are particularly important for external functions that
the optimizer can not analyze.</li>
<li>Use ptrtoint/inttoptr sparingly (they interfere with pointer aliasing
analysis), prefer GEPs</li>
<li>Use the lifetime.start/lifetime.end and invariant.start/invariant.end
intrinsics where possible.  Common profitable uses are for stack like data
structures (thus allowing dead store elimination) and for describing
life times of allocas (thus allowing smaller stack sizes).</li>
<li>Use pointer aliasing metadata, especially tbaa metadata, to communicate
otherwise-non-deducible pointer aliasing facts</li>
<li>Use the &#8220;most-private&#8221; possible linkage types for the functions being defined
(private, internal or linkonce_odr preferably)</li>
<li>Mark invariant locations using !invariant.load and TBAA&#8217;s constant flags</li>
<li>Prefer globals over inttoptr of a constant address - this gives you
dereferencability information.  In MCJIT, use getSymbolAddress to provide
actual address.</li>
<li>Be wary of ordered and atomic memory operations.  They are hard to optimize
and may not be well optimized by the current optimizer.  Depending on your
source language, you may consider using fences instead.</li>
<li>If calling a function which is known to throw an exception (unwind), use
an invoke with a normal destination which contains an unreachable
instruction.  This form conveys to the optimizer that the call returns
abnormally.  For an invoke which neither returns normally or requires unwind
code in the current function, you can use a noreturn call instruction if
desired.  This is generally not required because the optimizer will convert
an invoke with an unreachable unwind destination to a call instruction.</li>
<li>If you language uses range checks, consider using the IRCE pass.  It is not
currently part of the standard pass order.</li>
<li>For languages with numerous rarely executed guard conditions (e.g. null
checks, type checks, range checks) consider adding an extra execution or
two of LoopUnswith and LICM to your pass order.  The standard pass order,
which is tuned for C and C++ applications, may not be sufficient to remove
all dischargeable checks from loops.</li>
<li>Use profile metadata to indicate statically known cold paths, even if
dynamic profiling information is not available.  This can make a large
difference in code placement and thus the performance of tight loops.</li>
<li>When generating code for loops, try to avoid terminating the header block of
the loop earlier than necessary.  If the terminator of the loop header
block is a loop exiting conditional branch, the effectiveness of LICM will
be limited for loads not in the header.  (This is due to the fact that LLVM
may not know such a load is safe to speculatively execute and thus can&#8217;t
lift an otherwise loop invariant load unless it can prove the exiting
condition is not taken.)  It can be profitable, in some cases, to emit such
instructions into the header even if they are not used along a rarely
executed path that exits the loop.  This guidance specifically does not
apply if the condition which terminates the loop header is itself invariant,
or can be easily discharged by inspecting the loop index variables.</li>
<li>In hot loops, consider duplicating instructions from small basic blocks
which end in highly predictable terminators into their successor blocks.
If a hot successor block contains instructions which can be vectorized
with the duplicated ones, this can provide a noticeable throughput
improvement.  Note that this is not always profitable and does involve a
potentially large increase in code size.</li>
<li>Avoid high in-degree basic blocks (e.g. basic blocks with dozens or hundreds
of predecessors).  Among other issues, the register allocator is known to
perform badly with confronted with such structures.  The only exception to
this guidance is that a unified return block with high in-degree is fine.</li>
<li>When checking a value against a constant, emit the check using a consistent
comparison type.  The GVN pass <em>will</em> optimize redundant equalities even if
the type of comparison is inverted, but GVN only runs late in the pipeline.
As a result, you may miss the opportunity to run other important
optimizations.  Improvements to EarlyCSE to remove this issue are tracked in
Bug 23333.</li>
<li>Avoid using arithmetic intrinsics unless you are <em>required</em> by your source
language specification to emit a particular code sequence.  The optimizer
is quite good at reasoning about general control flow and arithmetic, it is
not anywhere near as strong at reasoning about the various intrinsics.  If
profitable for code generation purposes, the optimizer will likely form the
intrinsics itself late in the optimization pipeline.  It is <em>very</em> rarely
profitable to emit these directly in the language frontend.  This item
explicitly includes the use of the <a class="reference internal" href="../LangRef.html#int-overflow"><span>overflow intrinsics</span></a>.</li>
<li>Avoid using the <a class="reference internal" href="../LangRef.html#int-assume"><span>assume intrinsic</span></a> until you&#8217;ve
established that a) there&#8217;s no other way to express the given fact and b)
that fact is critical for optimization purposes.  Assumes are a great
prototyping mechanism, but they can have negative effects on both compile
time and optimization effectiveness.  The former is fixable with enough
effort, but the later is fairly fundamental to their designed purpose.</li>
</ol>
<p>p.s. If you want to help improve this document, patches expanding any of the
above items into standalone sections of their own with a more complete
discussion would be very welcome.</p>
</div>
<div class="section" id="adding-to-this-document">
<h2><a class="toc-backref" href="#id6">Adding to this document</a><a class="headerlink" href="#adding-to-this-document" title="Permalink to this headline">¶</a></h2>
<p>If you run across a case that you feel deserves to be covered here, please send
a patch to <a class="reference external" href="http://lists.llvm.org/mailman/listinfo/llvm-commits">llvm-commits</a> for review.</p>
<p>If you have questions on these items, please direct them to <a class="reference external" href="http://lists.llvm.org/mailman/listinfo/llvm-dev">llvm-dev</a>.  The more relevant
context you are able to give to your question, the more likely it is to be
answered.</p>
</div>
</div>


          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../MCJITDesignAndImplementation.html" title="MCJIT Design and Implementation"
             >next</a> |</li>
        <li class="right" >
          <a href="../GetElementPtr.html" title="The Often Misunderstood GEP Instruction"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="../index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2003-2015, LLVM Project.
      Last updated on 2015-09-08.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
</html>